const fanner = require('.');

/**
 * 最终执行函数，必须传递
 * @param {Object} item 
 *   {Object} item.reqs 请求参数{url:'',exec:function}
 *   {Object} item.config 请求配置{}
 * @return {Promise} 必须返回Promise，最终返回结果对象{code:1,...}
 */
function fetch(item) {
    console.log(`..... item=${JSON.stringify(item)}`);
    return new Promise((resolve, reject) => {
        resolve({ code: -1, reqs: item.reqs });
    });
}

/**
 * 对失败的数据对象进行处理，非必需
 * @param {array} items 
 */
function tryor(items) {
    if (items && items.length) {
        for (const item of items) {
            item.msg = 'oh no this is failed!'
        }
    }
    return items;
}

let p = {
    web: 'http://www.xxx.com/'
};

(async () => {
    console.time(`计时器`)

    let items = [];
    for (let i = 0; i < 10; i++) {
        items.push({
            reqs: {
                url: `${p.web}${i}`,
                page: i
            }
        })
    }

    let r = await fanner(items, { fetchor: fetch, tryor: tryor });
    console.log(JSON.stringify(r));

    console.timeEnd(`计时器`)
})();
